package com.liang.paul.officelite.activities;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.dd.processbutton.iml.ActionProcessButton;
import com.liang.paul.officelite.R;
import com.liang.paul.officelite.beams.OfficeUser;

import cn.bmob.v3.listener.EmailVerifyListener;
import cn.bmob.v3.listener.ResetPasswordByEmailListener;

public class ErroActivity extends FastActivity implements View.OnClickListener {

    EditText email;
    ActionProcessButton resend, changepwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_erro);
        initView();
    }

    private void initView() {
        email = f(R.id.editText_erro_email);
        resend = f(R.id.btn_erro_resendactivatemail);
        changepwd = f(R.id.btn_erro_changepwd);
        resend.setMode(ActionProcessButton.Mode.ENDLESS);
        changepwd.setMode(ActionProcessButton.Mode.ENDLESS);
        resend.setOnClickListener(this);
        changepwd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_erro_changepwd:
                changepwd.setProgress(1);
                changPWD();
                break;
            case R.id.btn_erro_resendactivatemail:
                resend.setProgress(1);
                resentEmail();
                break;
        }
    }

    public void changPWD(){
        OfficeUser.resetPasswordByEmail(this, email.getText().toString(), new ResetPasswordByEmailListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(getApplicationContext(), "重置密码邮件已发送，请查收", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
                changepwd.setProgress(0);
            }
        });
    }

    public void resentEmail(){
        OfficeUser.requestEmailVerify(this, email.getText().toString(), new EmailVerifyListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(getApplicationContext(), "验证邮件已发送，请查收", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(getApplicationContext(), "重置密码邮件已发送，请查收", Toast.LENGTH_SHORT).show();
                resend.setProgress(0);
            }
        });
    }
}
